package android.FiltrosJava;

import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;

public abstract class TransparencyMask extends AbstractImageBasic{
	int bgR, bgG, bgB;
	double minT, maxT;
	public TransparencyMask(){
		setBackgroundColor(0, 0, 0);
		settransparencyInterval(0.0f, 1.0f);
	}
	
	public TransparencyMask(double minTransparency, double maxTransparency, int R, int G, int B){
		setBackgroundColor(R, G, B);
		settransparencyInterval(minTransparency, maxTransparency);
	}
	
	public void settransparencyInterval(double minTransparency, double maxTransparency){
		minT = minTransparency;
		maxT = maxTransparency;		
	}
	
	public void setBackgroundColor(int R, int G, int B){
		bgR = R;
		bgG = G;
		bgB = B;		
	}
	
	public BufferedImage applyFilter(BufferedImage dst){
    	int dimX = dst.getWidth();
        int dimY = dst.getHeight();
    	
    	WritableRaster raster_dst = dst.getRaster();
    	int numBands = raster_dst.getNumBands();
    	int[] imageData = new int[numBands];
    	
    	//double[][] mask = computeMask(dimX, dimY);
    	float[][] mask = computeMask(dimX, dimY);
    	double p, pa; 
    	
    	for ( int y = 0; y < dimY; y++ ) {
    		for ( int x = 0; x < dimX; x++ ) {
    			raster_dst.getPixel(x,y,imageData);
    			
    			//p = mask[y][x];
    			p = (double)mask[y][x];
    			pa = 1.0f - p;
    			
    			imageData[0] = (int) (p * (float)imageData[0] + pa * (float)bgR);
    			imageData[1] = (int) (p * (float)imageData[1] + pa * (float)bgG);
    			imageData[2] = (int) (p * (float)imageData[2] + pa * (float)bgB);
    			
    			raster_dst.setPixel(x,y,imageData);    			
    		}
    	}    	
    	mask = null;
    	return dst;
    }
    
    //public abstract double[][] computeMask(int dimX, int dimY);
	public abstract float[][] computeMask(int dimX, int dimY);
}
